window._MJOB = {

    _CURRENTPARENTNODE:null,

    _ROLE:null,

    _JOB:null,

    _DEFAULTJOB:'无业',

    _MAINLABEL:null,

    _JOBARR:null,

    _PAGE:0,

    _CANSWITCH:false,

    _ISTJ:false,

    nextSkillMap:null,

    getNextSkillBd:function(skill){
        if(!_MJOB.nextSkillMap){
            var tempMap={};
            _MJOB.nextSkillMap={};
            for(var key in _SKILLBD){
                if(_SKILLBD[key].skilltype){
                    if(!tempMap[_SKILLBD[key].skilltype]){
                        tempMap[_SKILLBD[key].skilltype]=[];
                    }
                    tempMap[_SKILLBD[key].skilltype].push(key);
                }
            }

            for(var key in tempMap){
                for(var i=1;i<tempMap[key].length;i++){
                    _MJOB.nextSkillMap[tempMap[key][i-1]]=tempMap[key][i];
                }
            }
        }
        return _MJOB.nextSkillMap[skill];
    },

    showJobDes:function(param){
        _MJOB._JOB = _MJOB._DEFAULTJOB;
        _MJOB._ROLE = null;
        _MJOB._JOBARR=null;
        _MJOB._ISTJ = false;
        _MJOB._PAGE=0;

        var uiObj = _GRM.getNodeObj('jobDetails');
        var mainLabel = uiObj.node;
        
        if(uiObj.isnew){
            _MJOB.initButton(mainLabel);
        }

        if(param){
            _MJOB._ROLE = param.role;
            if(param.role){
                _MJOB._JOB = param.role.job;
            }

            if(param.job){
                _MJOB._JOB = param.job;
                _MJOB._ISTJ = true;
            }
        }

        
        _MJOB._CURRENTPARENTNODE.addChild(mainLabel);
        _MJOB._MAINLABEL = mainLabel;
        _MJOB.refreshLabel();

        if(param&&param.scal){
            mainLabel.setScale(param.scal,param.scal);
        }else{
            mainLabel.setScale(1,1);
        }

    },

    initButton:function(mainLabel){

        mainLabel.getChildByName('mask').on(cc.Node.EventType.TOUCH_START, function(){
            _GRM.putNode(this);
        }, mainLabel);

        mainLabel.getChildByName('btnSwtich').on(cc.Node.EventType.TOUCH_START, function(){
            //console.log('zhuanzhi');
            _MJOB.switchJob(_MJOB._ROLE,_MJOB._JOB);
        }, mainLabel);

        mainLabel.getChildByName('btnR').on(cc.Node.EventType.TOUCH_START, function(){
            //console.log('right');
            _MJOB.refreshPage(1);
        }, mainLabel);

        mainLabel.getChildByName('btnL').on(cc.Node.EventType.TOUCH_START, function(){
            //console.log('left');
            _MJOB.refreshPage(-1);
        }, mainLabel);

        var tabs = mainLabel.getChildByName('tab').getChildren();
        for(var i=0;i<tabs.length;i++){

            tabs[i].on(cc.Node.EventType.TOUCH_START, function(){
                console.log(this.getName());

                _MJOB.switchJobArr(this.getName());

            }, tabs[i]);

        }

    },

    refreshLabel:function(){

        var jobConfig = _JOB[_MJOB._JOB];
        var jobNpc = _NPC[jobConfig.face];
        var mainLabel = _MJOB._MAINLABEL;
        mainLabel.getChildByName('face').getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame(jobNpc.face);
        _CU.initAllAniNode(jobNpc.pf, mainLabel.getChildByName('ani'));
        mainLabel.getChildByName('des').getComponent(cc.Label).string = JOBHELPER.getJobDesc(_MJOB._JOB);
        mainLabel.getChildByName('btnSwtich').active = _MJOB._CANSWITCH&&!_MJOB._ISTJ;
        mainLabel.getChildByName('tab').active = _MJOB._CANSWITCH&&!_MJOB._ISTJ;
        mainLabel.getChildByName('btnR').active = _MJOB._CANSWITCH&&!_MJOB._ISTJ;
        mainLabel.getChildByName('btnL').active = _MJOB._CANSWITCH&&!_MJOB._ISTJ;
    },

    getTotalJobArr:function(){

        if(_MJOB._JOBARR){
            return _MJOB._JOBARR;
        }else{
            _MJOB._JOBARR = [];
            for(var key in _JOB){
                _MJOB._JOBARR.push(key);
            }
            return _MJOB._JOBARR;
        }

    },

    refreshPage:function(dir){

        var arr = _MJOB.getTotalJobArr();
        if(_MJOB._PAGE+dir>=arr.length){
            _MJOB._PAGE=0;
        }else if(_MJOB._PAGE+dir<0){
            _MJOB._PAGE = arr.length-1;
        }else{
            _MJOB._PAGE+=dir;
        }
        _MJOB._JOB = arr[ _MJOB._PAGE];
        _MJOB.refreshLabel();
    },

    setParentNode:function(node,canswitch){

        _MJOB._CURRENTPARENTNODE = node;
        _MJOB._CANSWITCH = canswitch;

    },


    switchJob:function(role,job){

        if(role.job==job){
            _TIPS.showMessage('该角色已经是此职业');
        }else{

            var jobConfig = _JOB[job];
            var currentJobConfig = _JOB[role.job];

            if(
              jobConfig
              &&currentJobConfig
              &&currentJobConfig.level>=jobConfig.level
            ){
              _TIPS.showMessage('只能转为更高阶的职业');
              return;
            }

            if(jobConfig&&jobConfig.xw){

                if(!XWH.isXwEnough(jobConfig,role)){
                    _TIPS.showMessage('修为不满足转职条件!\n（点击“修为”为关卡解锁对应修为,在书籍店购买修为书给角色习得,去对应的关卡杀怪增长修为）');
                    return;
                }

                //职业形象切换
                if(role.face==role.job&&role.pf==role.job){
                    role.pf = jobConfig.pf;
                    role.face = job;
                }
                role.job = job;
                

                //清理不符合职业的装备
                var equipArr = role.equip;
                role.equip=[];
                if(equipArr&&equipArr.length>0){
                    for(var i=0;i<equipArr.length;i++){
                        var wear = _EQUIPHELPER.canWear(role,equipArr[i].type,equipArr[i]);
                        if(wear.flag){
                            role.equip.push(equipArr[i]);
                        }else{
                            _GAMEDATAHELPER.pushItem(equipArr[i]);
                        }
                    }
                }

                //升级技能
                _MJOB.upJobSkill(role);

                _TIPS.showMessage('转职成功,技能和装备均有调整请注意');

                _TTCH.doCompleteTask('turnJob',null,true);
            }

        }

    },

    //获取转职后的技能
    upJobSkill:function(role){

      var newskillArr = _CROF.getSkillOfJob(role.job);

      if(!role.skill||role.skill.length<=0){

        role.skill=newskillArr;

      }else{

        var maxskillnmb = _SKILLHEPLER.getRoleSkillNmb(role);
        for(var j=0;j<role.skill.length;j++){
            var nextSkill = _MJOB.getNextSkillBd(role.skill[j]);
            if(nextSkill!=null){
                role.skill[j]=nextSkill;
            }
        }

        for(var i=0;i<newskillArr.length;i++){
            if(role.skill.length<maxskillnmb){
                role.skill.push(newskillArr[i]);
            }
        }
    
     }



    },

    switchJobArr:function(type){

        if(type=="全"){
            _MJOB._JOBARR=null;
        }else{
            _MJOB._JOBARR = [];
            for(var key in _JOB){

                if(_CU.isContain(_JOB[key].equipType,type)){

                    _MJOB._JOBARR.push(key);

                }

            }
        }
        var jobArr = _MJOB.getTotalJobArr();
        _MJOB._JOB = jobArr[0];
        _MJOB._PAGE=0;
        _MJOB.refreshLabel();
    }

}